package cn.hn.yh.controller.fhyc;

import cn.hn.yh.common.DataStory;
import cn.hn.yh.common.utils.DateUtil;
import cn.hn.yh.serviceInterface.fhyc.StDdFhycServiceInf;
import cn.hn.yh.vo.response.DataVO;
import cn.hn.yh.vo.response.FhycDataVO;
import cn.hn.yh.vo.response.ReturnVO;
import cn.hn.yh.vo.response.StateVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/fhyc/st")
public class fhycController {

    @Autowired
    private StDdFhycServiceInf stDdFhycServiceInf;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private DateUtil dateUtil;

    @GetMapping("/getStDdFhyc")
    public String getStDdFhyc(String pdate, String netid) {
        String str = "";
        List<String[]> list1 = new ArrayList<>();
        List<String[]> list2 = new ArrayList<>();
        List<String[]> data = stDdFhycServiceInf.getStDdFhyc(pdate, netid);
        String[] columnname = DataStory.DAY_LOAD_96COLUMN_NAME;
        list2.add(columnname);
        Map<String, Object> map = new HashMap<>();
        map.put("xAxisList", list2);
        String[] str1 = new String[data.size()];
        list1.add(data.get(1));
        list1.add(data.get(2));
        list1.add(data.get(0));
        try {
            map.put("data", list1);
            map.put("count", "96");
            str = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

    @GetMapping("/getStDdFhyc_table")
    public String getStDdFhyc_table(String pdate, String netid) {
        String obj = "";
        List<String[]> list = new ArrayList<>();
        List<String[]> data = stDdFhycServiceInf.getStDdFhyc(pdate, netid);
        String[] columnname = DataStory.DAY_LOAD_96COLUMN_NAME;
        for (int i = 0; i < columnname.length; i++) {
            String[] str = new String[3];
            str[0] = columnname[i];
            str[1] = data.get(1) == null ? null : data.get(1)[i];
            str[2] = data.get(2) == null ? null : data.get(2)[i];
            list.add(str);
        }
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("data", list);
            map.put("code", 0);
            map.put("msg", "");
            map.put("count", "96");
            obj = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return obj;
    }

    @GetMapping("/getSwyd_avg1")
    public String getStDdFhyc_avg1(String pdate1, String pdate2, String netid) {
        String str = "";
        List<String[]> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        List<String> dates = dateUtil.getDateList(pdate1, pdate2, "yyyy-MM-dd", "asc");
        for (int i = 0; i < dates.size(); i++) {
            String[] s = new String[4];
            String[] data = stDdFhycServiceInf.getStDdFhyc_avg1(dates.get(i), netid);
            s[0] = data[0];
            s[1] = dates.get(i);
            s[2] = data[1];
            if (data[2] != null) {
                Date date = DateUtil.format(data[2]);
                s[3] = DateUtil.format(date);
            } else {
                s[3] = "---";
            }
            list.add(s);
        }
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("code", 0);
            map.put("msg", "");
            map.put("count", "96");
            map.put("data", list);
            str = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

    @GetMapping("/getSwyd_avg2")
    public String getStDdFhyc_avg2(String pdate) {
        String str = "";
        List<String[]> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        List<String[]> data = stDdFhycServiceInf.getStDdFhyc_avg2(pdate);
        for (int i = 0; i < data.size(); i++) {
            String[] s = new String[4];
            s[0] = data.get(i)[0];
            s[1] = pdate;
            s[2] = data.get(i)[1];
            if (data.get(i)[2] != null) {
                Date date = DateUtil.format(data.get(i)[2]);
                s[3] = DateUtil.format(date);
            } else {
                s[3] = "---";
            }
            list.add(s);
        }
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("code", 0);
            map.put("msg", "");
            map.put("count", "96");
            map.put("data", list);
            str = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

    /*----------------------------------------------------------------------------------------*/
    @GetMapping("/vue_fhyc_table")
    public String getVueStFhyc(@RequestParam("pdate") String pdate, @RequestParam("netid") String netid) {
        System.out.println("pdate===" + pdate);
        System.out.println("netid===" + netid);
        String str = "";
        List<String[]> list1 = new ArrayList<>();
        List<String[]> list2 = new ArrayList<>();
        List<String[]> data = stDdFhycServiceInf.getStDdFhyc_vue(pdate, netid);
        String[] columnname = DataStory.DAY_LOAD_96COLUMN_NAME;
        list2.add(columnname);
        list1.add(data.get(1));
        list1.add(data.get(2));
        list1.add(data.get(0));
        List<String[]> dataList = new ArrayList<>();
        for (int i = 0; i < columnname.length; i++) {
            String[] str1 = new String[3];
            str1[0] = columnname[i];
            str1[1] = list1.get(0)[i] == null ? "---" : list1.get(0)[i];
            str1[2] = list1.get(1)[i] == null ? "---" : list1.get(1)[i];
            dataList.add(str1);
        }
        try {
            List<Object> dataVOList = new ArrayList<>();
            ReturnVO returnVO = new ReturnVO();
            StateVO stateVO = new StateVO();
            stateVO.setCode(200);
            stateVO.setMsg("success");
            for (int i = 0; i < dataList.size(); i++) {
                DataVO dataVO = new DataVO();
                dataVO.setPoint(dataList.get(i)[0]);
                dataVO.setPlan(dataList.get(i)[1]);
                dataVO.setReal(dataList.get(i)[2]);
                dataVOList.add(dataVO);
            }
            returnVO.setState(stateVO);
            returnVO.setData(dataVOList);
            str = objectMapper.writeValueAsString(returnVO);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

    @GetMapping("/vue_fhyc_multiAvg")
    public String getStFhyc_multiAvg(@RequestParam("start") String start, @RequestParam("end") String end, @RequestParam("netid") String netid) {
        String str = "";
        List<String[]> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        List<String> dates = dateUtil.getDateList(start, end, "yyyy-MM-dd", "asc");
        for (int i = 0; i < dates.size(); i++) {
            String[] s = new String[4];
            String[] data = stDdFhycServiceInf.getStDdFhyc_avg1(dates.get(i), netid);
            s[0] = data[0];
            s[1] = dates.get(i);
            s[2] = data[1];
            if (data[2] != null) {
                Date date = DateUtil.format(data[2]);
                s[3] = DateUtil.format(date);
            } else {
                s[3] = "---";
            }
            list.add(s);
        }
        ReturnVO returnVO = new ReturnVO();
        List<Object> fhycDataList = new ArrayList<>();
        StateVO stateVO = new StateVO();
        try {
            stateVO.setCode(200);
            stateVO.setMsg("success");
            for (int i = 0; i < list.size(); i++) {
                FhycDataVO fhycDataVO = new FhycDataVO();
                fhycDataVO.setCity(list.get(i)[0]);
                fhycDataVO.setDate(list.get(i)[1]);
                fhycDataVO.setAvg(list.get(i)[2]);
                fhycDataVO.setSubmit(list.get(i)[3]);
                fhycDataList.add(fhycDataVO);
            }
            returnVO.setState(stateVO);
            returnVO.setData(fhycDataList);
            str = objectMapper.writeValueAsString(returnVO);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

    @GetMapping("/vue_fhyc_avgRank")
    public String getStFhyc_avgRank(@RequestParam("pdate") String pdate) {
        String str = "";
        List<String[]> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        List<String[]> data = stDdFhycServiceInf.getStDdFhyc_avg2(pdate);
        for (int i = 0; i < data.size(); i++) {
            String[] s = new String[4];
            s[0] = data.get(i)[0];
            s[1] = pdate;
            s[2] = data.get(i)[1];
            if (data.get(i)[2] != null) {
                Date date = DateUtil.format(data.get(i)[2]);
                s[3] = DateUtil.format(date);
            } else {
                s[3] = "---";
            }
            list.add(s);
        }
        System.out.println(list.get(2)[2]);
        System.out.println(list.get(5)[2]);
        ReturnVO returnVO = new ReturnVO();
        List<Object> fhycDataList = new ArrayList<>();
        StateVO stateVO = new StateVO();
        try {
            stateVO.setCode(200);
            stateVO.setMsg("success");
            for (int i = 0; i < list.size(); i++) {
                FhycDataVO fhycDataVO = new FhycDataVO();
                fhycDataVO.setCity(list.get(i)[0]);
                fhycDataVO.setDate(list.get(i)[1]);
                fhycDataVO.setAvg(list.get(i)[2]);
                fhycDataVO.setSubmit(list.get(i)[3]);
                fhycDataList.add(fhycDataVO);
            }
            returnVO.setState(stateVO);
            returnVO.setData(fhycDataList);
            str = objectMapper.writeValueAsString(returnVO);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

    @GetMapping("/vue_st_fhyc_chart")
    public String getStFhycDaChart(String pdate, String netid) {
        String str = "";
        List<String[]> list1 = new ArrayList<>();
        List<String[]> list2 = new ArrayList<>();
        List<String[]> data = stDdFhycServiceInf.getStDdFhyc(pdate, netid);
        String[] columnname = DataStory.DAY_LOAD_96COLUMN_NAME;
        list2.add(columnname);
        Map<String, Object> map = new HashMap<>();
        map.put("xAxisList", list2);
        String[] str1 = new String[data.size()];
        list1.add(data.get(1));
        list1.add(data.get(2));
        list1.add(data.get(0));
        try {
            map.put("data", list1);
            map.put("count", "96");
            str = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }
}
